package org.lyl.taobao.controller;

import org.lyl.taobao.api.ICommodityService;
import org.lyl.taobao.api.IOrderService;
import org.lyl.taobao.api.IShopCartService;
import org.lyl.taobao.api.IUserService;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.api.entity.Order;
import org.lyl.taobao.api.entity.ShopCart;
import org.lyl.taobao.api.entity.User;
import org.lyl.taobao.service.CommodityServiceImpl;
import org.lyl.taobao.service.OrderServiceImpl;
import org.lyl.taobao.service.ShopCartServiceImpl;
import org.lyl.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Re
 * @Date: 2021/4/7 21:00
 */
@WebServlet("/addShopCart")
public class AddShopCartServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        IUserService iUserService = new UserServiceImpl();
        IShopCartService iShopCartService = new ShopCartServiceImpl();
        ICommodityService iCommodityService = new CommodityServiceImpl();
        IOrderService iOrderService = new OrderServiceImpl();
        if (req.getParameter("collect") != null) {
            Integer c_Id = Integer.parseInt(req.getParameter("collect"));
            Integer merchantId = iUserService.userIdByC_Id(c_Id);
            ShopCart shopCart = new ShopCart(userId,c_Id,merchantId);
            if (iShopCartService.addShopCart(shopCart)) {
                req.setAttribute("msg","已添加到购物车");
            } else {
                req.setAttribute("msg","添加购物车失败");
            }
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        } else if (req.getParameter("buy") != null){
            Integer c_Id = Integer.parseInt(req.getParameter("buy"));
            Integer merchantId = iUserService.userIdByC_Id(c_Id);
            Order order = new Order(userId,c_Id,merchantId);
            float balance = iUserService.getUserBalanceByUserId(userId);
            float price = iCommodityService.getCommodityPriceByC_Id(c_Id);
            if (balance >= price) {
                int num = 0;
                if (iUserService.reduceBalanceByUserId(price,userId)){
                    num++;
                }
                if (iUserService.addBalanceByUserId(price,merchantId)) {
                    num++;
                }
                if (num == 2){
                    if (iOrderService.addOrder(order)) {
                        req.setAttribute("msg","购买成功");
                        req.getRequestDispatcher("index.jsp").forward(req,resp);
                    }
                }
            } else {
                req.setAttribute("msg","您没钱了");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }
        } else if (req.getParameter("intoCollect") != null){
            List<Integer> c_Ids = iShopCartService.checkC_IdByUserId(userId);
            List<Commodity> commodities = new ArrayList<>();
            for (int i = 0; i < c_Ids.size(); i++) {
                Commodity commodity = iCommodityService.checkCommodityByC_Id(c_Ids.get(i));
                commodities.add(commodity);
            }
            HttpSession session1 = req.getSession();
            session1.setAttribute("commoditiesMsg",commodities);
            req.getRequestDispatcher("collect.jsp").forward(req,resp);
        } else if (req.getParameter("homepage") != null){
            req.getRequestDispatcher("homepage.jsp").forward(req,resp);
        } else {
            List<Order> orderList = iOrderService.checkOrder(userId);
            List<Commodity> commodityList = new ArrayList<>();
            for (int i = 0; i < orderList.size(); i++) {
                commodityList.add(iCommodityService.checkCommodityByC_Id(orderList.get(i).getOrderCommodityId()));
            }
            System.out.println(commodityList.size());
            HttpSession session2 = req.getSession();
            session2.setAttribute("orderListMsg",orderList);
            session2.setAttribute("commodityListMsg",commodityList);
            req.getRequestDispatcher("checkOrder.jsp").forward(req,resp);
        }
    }
}
